package com.core.bean;

import com.core.dao.IPasswordDao;
import com.core.dao.IUserDao;
import com.core.dao.mybatismysql.MybatisMysqlFactory;
import java.util.Date;
import java.util.List;

/**
 * 沈
 * 密码表
 */

public class Password {

    public static final int STATUS_NORMAL = 1;
    public static final int STATUS_STOP = 0;
    /**
     * 密码ID
     */
    private String id;
    /**
     * 用户id
     */
    private User user;
    /**
     * 密码
     */
    private String pwd;
    /**
     * 创建时间
     */
    private Date time;
    /**
     * 密码状态 0停用  1正常
     */
    private int status;

    public Password() {
    }

    public Password(String id, User user, String pwd, Date time, int status) {
        this.id = id;
        this.user = user;
        this.pwd = pwd;
        this.time = time;
        this.status = status;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Password{" +
                "id='" + id + '\'' +
                ", user=" + user +
                ", pwd='" + pwd + '\'' +
                ", time=" + time +
                ", status=" + status +
                '}';
    }

    /**
     * 检验密码是否有效。
     * 1、要求状态(status)为正常
     * 2、要求实例中的用户与密码和数据库中的一致
     * 满足以上2个条件返回真
     *
     * @return 放回真代表本实例的用户与密码正确并且有效。
     * @throws Exception 1、操作数据库失败会产生此异常。
     *                   2、用户名或密码为空会产生此异常。
     */
    private static final int STATUS=1;

    public boolean isActive() throws Exception {
        if (this.user.getName() == null || this.pwd == null)
            throw new RuntimeException("用户名或者密码不能为空");

        IPasswordDao passwordDao = MybatisMysqlFactory.create(MybatisMysqlFactory.MYBATIS_MYSQL_DAO).getPasswordDao();
        IUserDao userDao = MybatisMysqlFactory.create(MybatisMysqlFactory.MYBATIS_MYSQL_DAO).getUserDao();
        //通过用户名找到该user对象
        User byName = userDao.findByName(this.user.getName());
        //通过user的id找到多个密码
        List<Password> oldPwd = passwordDao.findOldPwd(byName.getId());
        if (oldPwd == null) {
            throw new RuntimeException("密码为空");
        }
        for (Password password : oldPwd) {
            //判断密码是否匹配且可以使用
            if (password.getPwd().equals(this.pwd) && password.getStatus() == STATUS) {
                return true;
            }
        }
        return false;
    }
}
